package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.entity.customer.CustomerInfo;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerInfoFeignClient feignClient;

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private CustomerInfoFeignClient customerInfoFeignClient;

    @Override
    public String login(String code) {
        // 调用service-customer服务，生成token，保存redis（key为user:login:"+token", value为远程调用返回的customId），并返回token
        Result<Long> result = feignClient.login(code);
        if (result.getCode() != 200){
            throw new GuiguException(result.getCode(), result.getMessage());
        }
        Long customId = result.getData();
        if (null == customId){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        String token = UUID.randomUUID().toString().replace("-", "");
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token, customId.toString(), RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
        return token;
    }

    @Override
    public CustomerInfo getCustomerLoginInfo(String token) {
        if (null == token){
            log.info("请求头中token为null,用户尚未登录");
            throw new RuntimeException();
        }
        Integer customId = (Integer) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
        if (null == customId){
            log.info("Redis中不存在用户id,用户身份已过期");
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        Result<CustomerInfo> result = feignClient.getCustomerInfo(Long.valueOf(customId));
        if (result.getCode() != 200) {
            throw new GuiguException(result.getCode(), result.getMessage());
        }
        CustomerInfo customerInfo = result.getData();
        if (null == customerInfo){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return customerInfo;
    }

    @Override
    public CustomerLoginVo getCustomerLoginInfo(Long customerId) {
        Result<CustomerLoginVo> customerLoginVoResult = customerInfoFeignClient.getCustomerLoginInfo(customerId);
        Integer code = customerLoginVoResult.getCode();
        if (code!=200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        CustomerLoginVo customerLoginVo = customerLoginVoResult.getData();
        if (customerLoginVo == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return customerLoginVo;
    }
}
